package com.suda.catalina.myapplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class SettingActivity extends AppCompatActivity {

    private Button button;
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Sqlite sqlite;
    private SQLiteDatabase sqLiteDatabase;
    private ContentValues contentValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        button = (Button) findViewById(R.id.createDatabase);
        button1 = (Button) findViewById(R.id.createData);
        button2 = (Button) findViewById(R.id.updateData);
        button3 = (Button) findViewById(R.id.deleteData);
        button4 = (Button) findViewById(R.id.retrieveData);
        sqlite = new Sqlite(SettingActivity.this, "user", null, 1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sqLiteDatabase = sqlite.getWritableDatabase();
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                contentValues = new ContentValues();
                contentValues.put("name", "long");
                contentValues.put("password", "123456");
                sqLiteDatabase.insert("user", null, contentValues);
                contentValues.clear();
                contentValues.put("name", "浪迹");
                contentValues.put("password", "123456");
                sqLiteDatabase.insert("user", null, contentValues);
                contentValues.clear();
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                contentValues.put("name", "langji");
                sqLiteDatabase.update("user", contentValues, "id=?", new String[]{"2"});
                contentValues.clear();
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sqLiteDatabase.delete("user", "id in (?, ?)", new String[]{"5", "6"});
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Cursor cursor = sqLiteDatabase.query("user", new String[]{"name", "password"}, null, null, null, null, "id desc");
                if (cursor.moveToFirst()) {
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String password = cursor.getString(cursor.getColumnIndex("password"));
                        Log.d("数据：", name + password);
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
